#include <QTGui>
#include "dlgImagePreview.h"

dlgImagePreview::dlgImagePreview(QString path, QWidget *parent) :
	QDialog(parent),m_path(path){
    setupUi(this);
	// m_path = path;

	imageLabel = new QLabel;
	imageLabel->setBackgroundRole(QPalette::Base);
	imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
	imageLabel->setScaledContents(true);

	scrollArea->setBackgroundRole(QPalette::Dark);
	scrollArea->setWidget(imageLabel);

	open();

	connect(toolButtonFit,SIGNAL(clicked()),this,SLOT(onSize_Fit()));
	connect(toolButtonNormalSize,SIGNAL(clicked()),this,SLOT(onSize_Normal()));
}

 void dlgImagePreview::onSize_Normal(void)
 {
	scrollArea->setWidgetResizable(false);
	imageLabel->adjustSize();
	m_scaleFactor = 1.0;
 }

 void dlgImagePreview::onSize_Fit(void)
 {
	scrollArea->setWidgetResizable(true);
 }

void dlgImagePreview::open()
{

	if (!m_path.isEmpty()) {
		QImage image(m_path);
		if (image.isNull()) {
			QMessageBox::information(this, tr("Image Viewer"),
									 tr("Cannot load %1.").arg(m_path));
			return;
		}

		imageLabel->setPixmap(QPixmap::fromImage(image));
		m_scaleFactor = 1.0;
		// imageLabel->adjustSize();
		// imageLabel->resize(m_scaleFactor * imageLabel->pixmap()->size());
		// imageLabel->adjustSize();
		/* printAct->setEnabled(true);
		 fitToWindowAct->setEnabled(true);
		 updateActions();

		if (!fitToWindowAct->isChecked())
			imageLabel->adjustSize();
		*/
	}
}

void dlgImagePreview::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        retranslateUi(this);
        break;
    default:
        break;
    }
}
